/**
 * 事件立案
 */
import moment from 'moment';
export default class extends think.BaseSocket {
  /**
     * 事件立案申请，添加案件
     */

  async applyCreateCaseAction() {
    const aj = think.ajaxJson();
    const data = this.post();
    const key = data.process_id;
    const id = data.org_id;
    const userid = data.userid;
    const overtime = data.overtime;
    const applyCaseService = this.service('oa/CaseService');
    const apiBpmnService = this.service('api/bpmn/BpmnService');
    const BpmnService = this.service('bpmn/BpmnService');
    const res = await BpmnService.startProcessInstance(key, id, userid,null,overtime);
    if (!res.error) {
      if (data.area_code && data.group_code) {
        data.ObjCode = data.area_code + data.group_code;
      }
      data.proc_inst_id = res.procInstId;
      const result = await applyCaseService.createCase(data);
      if (result) {
        // let toUser = this.getOnlineModuleByUser('jdsl');
        // let obj = this.getSocketMsgBody(res);
        // this.sendMsgByUserAction(obj,toUser,'acceptScreen');
        const userIds = res.backLogsUserIds;
        // let socketBody = {
        //     result: {
        //         title: res.review_name + ',待您审批',
        //         id: res.procInstId,
        //         date: res.date
        //     },
        //     moduleName: 'xtbg',
        //     userIds: userid,
        //     menuUrl: 'myTask'
        // };
        const socketBody = this.getSocketMsgBody();
        socketBody.result = {
          id: res.procInstId,
          date: res.date
        };
        socketBody.sendDate = moment().format('YYYY-MM-DD HH:mm:ss');
        socketBody.menuUrl = 'myTask';
        socketBody.sendUser = userid;
        socketBody.title = res.review_name + ',待您审批';
        if (userIds.length > 0) {
          this.sendMsgByUserAction(socketBody, userIds, 'msg_box');
          apiBpmnService.pushDataToBacklogPeople(userIds, res.form_type, res.data, res.review_name, res.procInstId);
        }
        aj.result = result;
      } else {
        aj.success = false;
        aj.msg = '申请立案失败';
      }
    } else {
      aj.success = false;
      aj.msg = '申请立案失败';
    }

    // 立案成功推送到web端
    const userIds = await this.getOnlineModuleByUser('jdzh');

    // let obj = this.getSocketMsgBody(result);
    // let socketBody = {
    //     result: {
    //         id: data.id,
    //         formModuleName: 'jdsl'
    //     },
    //     moduleName: 'jdzh',
    //     menuUrl: 'controlCenter'
    // };
    const socketBody = this.getSocketMsgBody();
    socketBody.result = {
      id: data.id,
      formModuleName: 'jdsl'
    };
    socketBody.title = '事件立案';
    socketBody.menuUrl = 'controlCenter';
    this.sendMsgByUserAction(socketBody, userIds, 'control_center');

    return this.successJson(aj);
  }
  async caseByIdAction() {
    const aj = think.ajaxJson();
    const data = this.post();
    const applyCaseService = this.service('oa/CaseService');
    const result = await applyCaseService.caseById(data);
    if (!result.error) {
      aj.result = result;
    } else {
      aj.success = false;
      aj.msg = '查询失败';
    }
    return this.json(aj);
  }

  /**
     * 部件立案申请
     */
  async applyPartCaseAction() {
    const aj = think.ajaxJson();
    const data = this.post();

    if (!data.case_id) {
      data.case_id = think.uuid('v1');
    }
    const key = data.process_id;
    const id = data.org_id;
    const userid = data.userid;
    const overtime = data.overtime;
    const BpmnService = this.service('bpmn/BpmnService');
    const applyCaseService = this.service('oa/CaseService');
    const apiBpmnService = this.service('api/bpmn/BpmnService');

    const res = await BpmnService.startProcessInstance(key, id, userid,null,overtime);

    if (!res.error) {
      data.proc_inst_id = res.procInstId;
      const result = await applyCaseService.createPartCase(data);
      if (!result.error) {
        // let socketBody = {
        //     result: {
        //         id: data.case_id,
        //         formModuleName: 'jdsl'
        //     },
        //     moduleName: 'jdzh',
        //     menuUrl: 'controlCenter'
        // };
        //
        // this.sendMsgByModuleAction(socketBody);
        const userIds = res.backLogsUserIds;
        // let socketBody = {
        //     result: {
        //         id: res.procInstId,
        //         date: res.date
        //     },
        //     moduleName: 'xtbg',
        //     userIds: userid,
        //     menuUrl: 'myTask'
        // };
        const socketBody = this.getSocketMsgBody();
        socketBody.result = {
          id: res.procInstId,
          date: res.date
        };
        socketBody.menuUrl = 'myTask';
        socketBody.sendUser = userid;
        socketBody.sendDate = moment().format('YYYY-MM-DD HH:mm:ss');
        socketBody.title = res.review_name + ',待您审批';
        if (userIds.length > 0) {
          this.sendMsgByUserAction(socketBody, userIds, 'msg_box');
          apiBpmnService.pushDataToBacklogPeople(userIds, res.form_type, res.data, res.review_name, res.procInstId);
        }
        aj.result = res;
      } else {
        aj.success = false;
        aj.msg = '申请立案失败';
      }
    } else {
      aj.success = false;
      aj.msg = '申请立案失败';
    }
    // 立案成功推送到web端
    const userIds = await this.getOnlineModuleByUser('jdzh');
    // 就事件随机推送给某个用户
    // const randomId = userIds[Math.floor(Math.random() * userIds.length)];

    // console.log(randomId, 'randomId');
    // let obj = this.getSocketMsgBody(result);
    const socketBody = this.getSocketMsgBody();
    socketBody.result = {
      id: data.id,
      formModuleName: 'jdsl'
    };
    socketBody.title = '事件立案';
    socketBody.menuUrl = 'controlCenter';
    this.sendMsgByUserAction(socketBody, userIds, 'control_center');

    return this.successJson();
  }

  /**
     * 更新部件表BGID: updateBGID
     */

  async updateCaseBGIDAction() {
    const aj = think.ajaxJson();
    const data = this.post();
    const updateCaseBGIDService = this.service('oa/CaseService');
    const res = await updateCaseBGIDService.updateCaseBGID(data);

    if (!res.error) {
      aj.result = res;
    } else {
      aj.success = false;
      aj.msg = '更新失败';
    }

    return this.json(aj);
  }

  /**
     * 查询案件表列表
     */

  async get_all_case_listAction() {
    const data = this.post();
    try {
      const res = await this.service('oa/CaseService').all_case_list(data);
      return this.successJson(res);
    } catch(error) {
      return this.errorJson('查询失败');
    }
  }

  /**
     * 条件查询案件列表
     */

  async get_specific_case_listAction() {
    const data = this.post();
    try {
      const res = await this.service('oa/CaseService').specific_case_list(data);
      return this.successJson(res);
    } catch (error) {
      return this.errorJson('查询失败');
    }
  }

  /**
     * 删除案件
     */
  async delete_caseAction() {
    const data = this.post();
    try {
      const res = await this.service('oa/CaseService').delete_case(data);
      return this.successJson(res);
    } catch (error) {
      return this.errorJson('删除失败');
    }
  }
}
